import java.util.ArrayList;
import java.util.Scanner;

/**
 * This program finds prime numbers till a number entered by a user
 *
 * @author Sergii Koziumynskyi
 * @version 0.03 2014-01-10
 */

public class PrimeNumbers {
    public static void main(String[] args) {

        // ask user about input and try to convert it to expected format
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number from 2 to 100,000.");
        int checkedInput = 0;
        boolean invalidInput = false;
        if (in.hasNextInt()) {
            checkedInput = in.nextInt();
        } else {
            invalidInput = true;
        }
        in.close();

        // stop execution if  input is invalid
        if (invalidInput) {
            System.out.println("Entered string is not an integer number.");
        } else if (checkedInput < 2 || checkedInput > 100000) {
            System.out.println("The number should be in 2 - 100,000 diapason.");

        // OR continue execution if entered value meets the criteria
        } else {
            ArrayList<Integer> numbers = getPrimeNumbers(checkedInput);
            System.out.println(numbers);
        }
    }

    /**
     * Method gets an integer number and returns a list of prime numbers between 2 and the number
     *
     * @param anInteger an integer with
     * @return an ArrayList with prime numbers
     */
    private static ArrayList<Integer> getPrimeNumbers(int anInteger) {
        ArrayList<Integer> result = new ArrayList<Integer>();

        for (int i = 2; i <= anInteger; i++) {
            result.add(i);
        }

        for (int i = 0; i < result.size() - 1; i++) { // -1 because there is no sense in check of the last element
            for (int j = i + 1; j < result.size(); j++) {
                if (result.get(j) % result.get(i) == 0) {
                    result.remove(j);
                    j--; // check current index again because it contains a new value
                }
            }
        }
        return result;
    }
}